package com.mall.service.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mall.model.domain.OmsCompanyAddress;
import com.mall.model.domain.OmsOrderReturnApply;
import com.mall.model.vo.OmsReturnApplyVo;
import com.mall.model.domain.OmsUpdateStatusParam;
import com.mall.service.order.mapper.OmsOrderReturnApplyMapper;
import com.mall.service.order.service.OmsCompanyAddressService;
import com.mall.service.order.service.OmsOrderReturnApplyService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class OmsOrderReturnApplyServiceImpl extends ServiceImpl<OmsOrderReturnApplyMapper, OmsOrderReturnApply> implements OmsOrderReturnApplyService {

    @Autowired
    OmsCompanyAddressService omsCompanyAddressService;

    @Autowired
    OmsOrderReturnApplyService omsOrderReturnApplyService;


    /**
     * 获取退货申请详情
     * @param id
     * @return
     */
    @Override
    public OmsReturnApplyVo getByIdWithAddress(Long id) {
        OmsOrderReturnApply orderReturnApply = this.getById(id);
        Long companyAddressId = orderReturnApply.getCompanyAddressId();
        OmsReturnApplyVo omsReturnApplyVo = new OmsReturnApplyVo();
        BeanUtils.copyProperties(orderReturnApply,omsReturnApplyVo);
        LambdaQueryWrapper<OmsCompanyAddress> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(OmsCompanyAddress::getId,companyAddressId);
        OmsCompanyAddress address = omsCompanyAddressService.getOne(queryWrapper);
        omsReturnApplyVo.setCompanyAddress(address);
        return omsReturnApplyVo;
    }

    /**
     * 修改退货申请状态
     * @param id
     * @param omsUpdateStatusParam
     */
    @Override
    public void updateReturnApplyStatus(Long id, OmsUpdateStatusParam omsUpdateStatusParam) {
        OmsOrderReturnApply byId = omsOrderReturnApplyService.getById(id);
        byId.setStatus(omsUpdateStatusParam.getStatus());
        byId.setCompanyAddressId(omsUpdateStatusParam.getCompanyAddressId());
        byId.setHandleMan(omsUpdateStatusParam.getHandleMan());
        byId.setHandleNote(omsUpdateStatusParam.getHandleNote());
        byId.setId(omsUpdateStatusParam.getId());
        byId.setReceiveMan(omsUpdateStatusParam.getReceiveMan());
        byId.setReceiveNote(omsUpdateStatusParam.getReceiveNote());
        byId.setReturnAmount(omsUpdateStatusParam.getReturnAmount());
        omsOrderReturnApplyService.updateById(byId);
    }
}
